/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  ProductHelper.java                                      
 *  File Contents Outline :  产品持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbProduct;
import storage.util.StorageManager;


public class ProductHelper {

    /**
     * 将product保存到LaUDMS数据库中
     *
     * @param product 要存储的模型对象
     */
    public void saveProduct(DbProduct product) {
 //       StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_product);
                                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_product"));
//        sm.open();
        sm.setRowKey(product.getRowKey());
        sm.put(DbProduct.cl_name, product.getName());
        sm.put(DbProduct.cl_updateTime, product.getUpdateTime());
        sm.put(DbProduct.cl_chinese, product.getChinese());
        ArrayList<String> logBodyList = product.getLogBodyList();
        for (int i = 0; i < logBodyList.size(); i++) {
            sm.put(DbProduct.cl_logBody + "_" + logBodyList.get(i), logBodyList.get(i));
        }
//        sm.close();

    }

    /**
     * 将product保存到LaUDMS数据库中
     *
     * @param product 要存储的模型对象
     */
    public void updateProduct(DbProduct product) {
                                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_product"));
//        sm.open();
        sm.delete(product.getRowKey());
        sm.setRowKey(product.getRowKey());
        sm.put(DbProduct.cl_name, product.getName());
        sm.put(DbProduct.cl_updateTime, product.getUpdateTime());
        if(product.getChinese()==null){
            product.setChinese("");
        }
        sm.put(DbProduct.cl_chinese, product.getChinese());
        ArrayList<String> logBodyList = product.getLogBodyList();
        for (int i = 0; i < logBodyList.size(); i++) {
            sm.put(DbProduct.cl_logBody + "_" + logBodyList.get(i), logBodyList.get(i));
        }
//        sm.close();

    }

    /**
     * 将一系列logHead保存到LaUDMS数据库中
     *
     * @param logHeads 要存储的模型对象列表
     */
    public void saveProducts(List<DbProduct> products) {
                                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_product"));
//        sm.open();
        for (int i = 0; i < products.size(); i++) {
            DbProduct product = products.get(i);
            sm.setRowKey(product.getRowKey());
            sm.put(DbProduct.cl_name, product.getName());
            sm.put(DbProduct.cl_updateTime, product.getUpdateTime());
            sm.put(DbProduct.cl_chinese, product.getChinese());
            ArrayList<String> logBodyList = product.getLogBodyList();
            for (int j = 0; j < logBodyList.size(); j++) {
                sm.put(DbProduct.cl_logBody + "_" + logBodyList.get(i), logBodyList.get(j));
            }
        }
//        sm.close();
    }

    public List<DbProduct> getProducts(final String columnName, final String value) {
        List<DbProduct> result = new LinkedList<DbProduct>();
                                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_product"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, chinese = null, updateTime = null;
                ArrayList<String> logBodyList = new ArrayList();
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbProduct.cl_chinese)) {
                        chinese = columnsValue;
                    } else if (columnsName.equals(DbProduct.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbProduct.cl_updateTime)) {
                        updateTime = columnsValue;
                    } else if (columnsName.startsWith(DbProduct.cl_logBody)) {
                        logBodyList.add(columnsValue);
                    }
                }
                DbProduct pProduct = new DbProduct(rowKey, name, chinese, updateTime, logBodyList);
                result.add(pProduct);
            }
        } catch (Exception ex) {
            Logger.getLogger(DbProduct.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public ArrayList<String> getAllProductName() {
                                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_product"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> productNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = "";
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbProduct.cl_name)) {
                        name = columnsValue;
                    }
                }
                if (!productNameList.contains(name)&&name.length()>0) {
                    productNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(DbProduct.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return productNameList;
    }

    public void deleteProduct(final String rowKey) {
                                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_product"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }

    /**
     * 为产品添加日志体字段
     *
     * @param product 产品
     * @param logBody 日志体字段
     */
    public void addLogBody(DbProduct product, String logBody) {
                                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_product"));
//        sm.open();
        sm.setRowKey(product.getRowKey());
        sm.put(DbProduct.cl_logBody + "_" + logBody, logBody);
//        sm.close();

    }

//    /**
//     *
//     */
//    public void deleteLogBody(DbProduct product, String logBody) {
//        StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_product);
////        sm.open();
//        sm.setRowKey(product.getRowKey());
//        // sm.put(DbProduct.cl_logBody + "_" + logBody, logBody);
//        sm.delete(DbProduct.cl_logBody + "_" + logBody, logBody);
////        sm.close();
//
//    }
    public void deleteLogBody(String productName, String logBodyname) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
